package com.example.javaapitest.data;

import java.sql.*;
import java.util.*;

public class DBData {

	public static List<Map<String, String>> dBData_mysql(String ip, String port, String baseName,
	                                   String userName, String password, String sql) throws ClassNotFoundException, SQLException {

		ResultSet rs;
		ResultSetMetaData rd;
		Class.forName("com.mysql.cj.jdbc.Driver");
		String url = String.format("jdbc:mysql://%s:%s/%s", ip, port, baseName);
		Connection conn = DriverManager.getConnection(url, userName, password);
		Statement createStatement = conn.createStatement();

		rs = createStatement.executeQuery(sql);
		rd = rs.getMetaData();
		List<Map<String, String>> paramList = new ArrayList<Map<String, String>>();
		while (rs.next()) {
			Map<String, String> map = new LinkedHashMap<String, String>();
			for (int i = 1; i < rd.getColumnCount(); i++) {
				map.put(rd.getColumnName(i), rs.getString(i));
			}
			paramList.add(map);
		}
		return paramList;
	}
}
